import time,sys,math,re,os
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
desired_caps = {
  "platformName": "Android",
  "deviceName": "",
  #模拟器
  "platformVersion": "7.1.2",
  #雷电
  # "deviceName": "emulator-5554",
  #逍遥
  # "deviceName": "192.168.2.33:5555",
  "appPackage": "",
  "appActivity": "",
  #真机
  # "platformVersion": "10",
  # "deviceName": "UMXDU20A10009758",#usb连接
  # "deviceName": "192.168.2.78:5555",#无线连接
  #自动授权
  "autoGrantPermissions":True,
  #使用引擎，默认为Appium
  "automationName": "UiAutomator2",
  "newCommandTimeout": 305,
  #不需要再次签名
  "noSign": True,
  #不需要再次签名
  "noReset": True,
  "adbExecTimeout": 200000,
  #不需要重新安装 io.appium.uiautomator2.server.apk 和 io.appium.uiautomator2.server.test.apk
  #当设备上没有uiautomator2包时，不能设置skipServerInstallation：True
  "skipServerInstallation":True,
  "recreateChromeDriverSessions":True,
  "resetKeyboard" : True,
  "'settings[waitForIdleTimeout]": 100
}
server = "http://localhost:4723/wd/hub"
appList = {"1":"yingsheng"}
baseConf = {}
# 判别调试程序
inputVal = ''
if len(sys.argv) <= 1:
  inputVal = input("输入数字：\n1.硬声APP\n")
  while inputVal not in ['1','2','3']:
    del inputVal
    inputVal = input("请重新输入!\n")

if (len(sys.argv) > 1 and sys.argv[1] == appList['1']) or inputVal == '1':
  baseConf['name'] = '硬声APP'
  baseConf['enName'] = appList[inputVal] if inputVal is not '' else sys.argv[1]
  desired_caps['appPackage'] = 'com.hq.hardvoice'
  desired_caps['appActivity'] = 'com.dy.njyp.mvp.ui.activity.login.WelcomeActivity'
else:
  print('调试程序参数错误！')
  sys.exit()
print(baseConf)

inputVal = input("输入数字：\n1.雷电模拟器\n2.逍遥模拟器\n3.真机调试模式\n4.真机无线模式\n5.夜神模拟器\n6.夜神模拟器-1\n7.魅蓝真机模式\n")
while desired_caps['deviceName'] is '':
  if inputVal == '1':
    print('进入雷电模拟器调试')
    desired_caps['deviceName'] = 'emulator-5554'
  elif inputVal == '2':
    print('进入逍遥模拟器调试')
    desired_caps['deviceName'] = '192.168.2.33:5555'
  elif inputVal == '3':
    print('进入真机调试')
    desired_caps['platformVersion'] = ''
    desired_caps['deviceName'] = ''#真机deviceName
  elif inputVal == '4':
    print('进入真机无线调试')
    desired_caps['platformVersion'] = '10'
    desired_caps['deviceName'] = '192.168.2.78:5555'
  elif inputVal == '5':
    print('进入夜神模拟器调试')
    desired_caps['platformVersion'] = '9'
    desired_caps['deviceName'] = '127.0.0.1:62001'
  elif inputVal == '6':
    print('进入夜神模拟器-1调试')
    desired_caps['platformVersion'] = '9'
    desired_caps['deviceName'] = '127.0.0.1:62025'
  elif inputVal == '7':
    print('进入魅蓝真机调试')
    desired_caps['platformVersion'] = '7.0'
    desired_caps['deviceName'] = '621QEQPG4FESP'
    #desired_caps['ignoreHiddenApiPolicyError'] = 'True'
  elif desired_caps['deviceName'] == '':
    del inputVal
    inputVal = input("请重新输入!\n")
try:
  driver = webdriver.Remote(server, desired_caps)
except Exception as e:
  print("错误重试",e)
  driver = webdriver.Remote(server, desired_caps)
import json
print(json.loads(json.dumps(desired_caps)))
# print(driver)
# sys.exit()

def isElement(identifyBy, c):
  flag = None
  try:
    if identifyBy == "id":
      # driver.implicitly_wait(60)
      driver.find_element_by_id(c)
    elif identifyBy == "xpath":
      # driver.implicitly_wait(60)
      driver.find_element_by_xpath(c)
    elif identifyBy == "class":
      driver.find_element_by_class_name(c)
    elif identifyBy == "link text":
      driver.find_element_by_link_text(c)
    elif identifyBy == "partial link text":
      driver.find_element_by_partial_link_text(c)
    elif identifyBy == "name":
      driver.find_element_by_name(c)
    elif identifyBy == "tag name":
      driver.find_element_by_tag_name(c)
    elif identifyBy == "css selector":
      driver.find_element_by_css_selector(c)
    flag = True
  except :
    flag = False
  finally:
    return flag